package com.kasax.skinchange

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.LayoutInflaterCompat
import com.kasax.skinchange.utils.FileUtils
import com.kasax.skinchange.utils.LogUtils
import com.kasax.skinchange.utils.permissions.PermissionUtils
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date

class UseFactory2Activity : AppCompatActivity() {
       private lateinit var layoutInfalatorFactory2: LayoutInfalatorFactory2

    private val sdf = SimpleDateFormat("yyyyMMddHHmmssss")
    private var filePath: String = ""
    private var testData: String = ""

    private var hasSdCardPermission: Boolean = false
    private lateinit var llContainer: LinearLayout
    private var titleCount: Int = 0
    private var callbackImpl = object : PermissionUtils.HandleCallback {
        override fun skipPermissionRequest() {
            hasSdCardPermission = true
        }

        override fun noPermission() {

        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        layoutInfalatorFactory2 = LayoutInfalatorFactory2(delegate)
        // 收集需要换肤的控件以及属性
        LayoutInflaterCompat.setFactory2(layoutInflater, layoutInfalatorFactory2)

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_use_factory2)
        setTitle("使用Factory2换肤")
        llContainer = findViewById<LinearLayout>(R.id.ll_container)
        testData = resources.getString(R.string.test_data)

        PermissionUtils.injectContext(this)
        PermissionUtils.requestSdcardPermission(callbackImpl)
    }


    fun handleSkinChange() {
        layoutInfalatorFactory2.changeSkin(this)
    }

    fun handleChangeSkin(view: View) {
        val apkFile = File(getExternalFilesDir(null), "SkinPack-debug.apk")
        SkingLoader.instance.loaderResourceWithNoReflection(this, apkFile.absolutePath)
        handleSkinChange()
    }

    fun handleResetSkin(view: View) {
        SkingLoader.instance.reset()
        handleSkinChange()
    }

    fun handleAddTextV(view: View) {
        val textView = TextView(this)
        val addAttr = layoutInfalatorFactory2.dynamicAddSkin(textView)
            .addAttr("text", R.string.test_string)
            .addAttr("textColor", R.color.skin_test_color)
        llContainer.addView(
            textView,
            ViewGroup.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
            )
        )
        layoutInfalatorFactory2.changeAttrView(this, addAttr)
        titleCount++
    }

    fun testWriteSampleData() {
        val time: String = sdf.format(Date())
        val currentFileName = "$time.log"

        filePath = "/mnt/sdcard/$currentFileName"
        filePath = FileUtils.getSafeSdcardPath(filePath)
        LogUtils.e("handleSampleFileWrite filePath: $filePath")
        FileUtils.write(testData, filePath)
    }

    fun testReadSampleData() {
        val data: String = FileUtils.readString(filePath)
        LogUtils.e("data: $data")
//        binding?.tvResult?.text = data
    }


    fun handleWriteFileTest(view: View) {
        if (hasSdCardPermission) {
            testWriteSampleData()
        }
    }

    fun handleReadFileTest(view: View) {
        if (hasSdCardPermission) {
            testReadSampleData()
        }
    }

    @RequiresApi(Build.VERSION_CODES.R)
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        PermissionUtils.handleIntentPermissionRequestResult(requestCode, resultCode, callbackImpl)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionUtils.handleOnRequestPermissionsResult(requestCode, permissions, false, callbackImpl)
    }
}